//
//  MonitorViewModel.m
//  CityFire
//
//  Created by Liuyz on 2017/8/16.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "MonitorViewModel.h"
#import "UserInfoModel.h"
#import "NSUserDefaults+Tools.h"
//#import "<#header#>"

@implementation MonitorViewModel
    
//#define         @"/api-w/warning/deal"                          //提交告警处理请求
    //table_name=device_record20170705,device_record20170705&num=1,2&record_usr_log=测试&misinformation=1
    
- (void)getWarningDeal:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        
        NSMutableDictionary *dictionary =[[NSMutableDictionary alloc]init];
        [dictionary setObject:model.seeeionID forKey:@"sessionid"];
        [dictionary setObject:model.userName forKey:@"username"];
        
        [dictionary setObject:model.userName forKey:@"table_name"];
        
        [YQNetworking requestWithUrl:kWARNING_DEAL_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"table_name" : @"device_record20170705,device_record20170705", @"num" : @"1,2", @"record_usr_log" : parameter[@"record_usr_log"] ?parameter[@"record_usr_log"] : @"", @"misinformation" : parameter[@"misinformation"] ?parameter[@"misinformation"] : @""} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        /api-w/warning/queryRealTimeWarning?
            //        username=admin&x_day=7&pageNo=1&pageSize=5
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            //        NSArray *monitArray = [MTLJSONAdapter modelsOfClass:[MonitorModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            //        self.monitorArray = monitArray;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                [SVProgressHUD showSuccessWithStatus:@"提交成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
   
    
}


- (void)getQueryRealTimeWarning:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kQUERY_REAL_WARNING_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"x_day" : [NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS], @"pageNo" : @"1", @"pageSize" : @"200"} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        /api-w/warning/queryRealTimeWarning?
            //        username=admin&x_day=7&pageNo=1&pageSize=5
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            NSArray *monitArray = [MTLJSONAdapter modelsOfClass:[MonitorModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.monitorArray = monitArray;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            NSLog(@"加载失败");
            //[SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    

}

- (void)getQueryRealTimeWarningCount:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kQUERY_REAL_WARNINGCOUNT_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"userName" : model.userName, @"x_day" : [NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //        /api-w/warning/queryRealTimeWarning?
            //        username=admin&x_day=7&pageNo=1&pageSize=5
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            MonitorCountModel *countModel = [MTLJSONAdapter modelOfClass:[MonitorCountModel class]  fromJSONDictionary:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.countModel = countModel;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            //[SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
}

- (void)getQueryHistoryWarning:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kWARNING_QueryHistoryWarning_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"x_day" : [NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS], @"pageNo" : @"1", @"pageSize" : @"200", @"start_date" : parameter[@"start_date"] ? : @"", @"end_date" : parameter[@"end_date"] ? : @"", @"type" : parameter[@"type"] ? : @"",@"keywords":parameter[@"keywords"]?parameter[@"keywords"]:@""} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //username=admin&x_day=7&pageNo=1&pageSize=5&sessionid=1&start_date=20170725&end_date=20170731
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            NSArray *monitArray = [MTLJSONAdapter modelsOfClass:[MonitorModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.monitorArray = monitArray;
            DDLogInfo(@"history info:%@",monitArray);
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:@"服务异常"];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:@"网络异常"];
        }];
    }
    
    
}

- (void)getAllDeviceInfo:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kDEVICES_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"userName" : model.userName, @"pageNo" : parameter[@"pageNo"], @"pageSize" : parameter[@"pageSize"]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            //userName=admin&pageNo=1&pageSize=10&sessionid=x
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            NSArray *monitArray = [MTLJSONAdapter modelsOfClass:[MonitorModel class] fromJSONArray:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.monitorArray = monitArray;
            //
            //        if (userModel) {
            //            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            //            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
            //        }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
    
}



@end
